package com.qiaofang.housewarehouse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.housewarehouse.base.util.BeanUtil;
import com.qiaofang.housewarehouse.dao.ESSearchHouseDao;
import com.qiaofang.housewarehouse.dao.EsDAO;
import com.qiaofang.housewarehouse.dao.HbaseDao;
import com.qiaofang.housewarehouse.dao.domain.EsHswhSaleHouse;
import com.qiaofang.housewarehouse.dao.domain.HswhSaleHouse;
import com.qiaofang.housewarehouse.dao.utils.ConfigUtils;
import com.qiaofang.housewarehouse.service.SaleHouseQueryInfoService;
import com.qiaofang.housewarehouse.service.compose.HswhSaleHouseCompose;
import com.qiaofang.housewarehouse.stub.bean.HousingAcqAttentionDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleHouseDetailDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleQueryResponseDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleQueryResponseEntity;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class SaleHouseQueryInfoServiceImpl implements SaleHouseQueryInfoService{
    private static final Logger logger = LoggerFactory.getLogger(SaleHouseQueryInfoServiceImpl.class);
    @Resource
    private HbaseDao hbaseDao;
    @Resource
    private EsDAO esDAO;
    @Resource
    private ESSearchHouseDao essDAO;
    @Override
    public HswhSaleQueryResponseEntity searchSaleHouseList(Map<String, String> paramMap) throws Exception {
        paramMap.put("indexName", ConfigUtils.getIndexHswhSaleHouse());
        paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
        logger.info("paramMap：{}", paramMap.toString());
        HswhSaleQueryResponseEntity hswhSaleQueryResponseEntity = new HswhSaleQueryResponseEntity();
        SearchResponse searchResponse = esDAO.searchHouseList(paramMap);
        logger.info("查询eslticsearch succee");
        if (searchResponse != null) {
            SearchHits hits = searchResponse.getHits();
            SearchHit[] hits1 = hits.getHits();
            List<EsHswhSaleHouse> saleHouses = new ArrayList<>();
            for (SearchHit hit : hits1) {
                Map<String, Object> source = hit.getSource();
                EsHswhSaleHouse hswhSaleHouse = BeanUtil.mapperObj(source, EsHswhSaleHouse.class);
                hswhSaleHouse.setId(hit.getId());
                saleHouses.add(hswhSaleHouse);
            }
 //           logger.info("从es中获取的二级索引：{}", rowkeys.toString());
 //           List<HswhSaleHouse> hswhSaleHouses = hbaseDao.listObjByRowKeys(TableConstants.HSWH_SALE_HOUSE, rowkeys, HswhSaleHouse.class);
            hswhSaleQueryResponseEntity.setHswhSaleHouseListEntity(HswhSaleHouseCompose.composeListHouseListEntity1(saleHouses));
            if (saleHouses.size() == 0) {
                hswhSaleQueryResponseEntity.setTotalCount(String.valueOf(0));
            } else {
                hswhSaleQueryResponseEntity.setTotalCount(String.valueOf(hits.getTotalHits()));
            }
            hswhSaleQueryResponseEntity.setPageIndex(paramMap.get("pageIndex"));
        }
        return hswhSaleQueryResponseEntity;
    }

    @Override
    public HswhSaleQueryResponseEntity searchSaleHouseList(Map<String, String> paramMap, List<HousingAcqAttentionDTO> housingAcqAttentionDTOS) throws Exception {

        paramMap.put("indexName", ConfigUtils.getIndexHswhSaleHouse());
        paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
        logger.info("paramMap：{}", paramMap.toString());
        HswhSaleQueryResponseEntity hswhSaleQueryResponseEntity = new HswhSaleQueryResponseEntity();
        SearchResponse searchResponse = essDAO.searchHouseList(paramMap,housingAcqAttentionDTOS,"sell");
        logger.info("查询eslticsearch succee");
        if (searchResponse != null) {
            SearchHits hits = searchResponse.getHits();
            SearchHit[] hits1 = hits.getHits();
            List<EsHswhSaleHouse> saleHouses = new ArrayList<>();
            for (SearchHit hit : hits1) {
                Map<String, Object> source = hit.getSource();
                EsHswhSaleHouse hswhSaleHouse = BeanUtil.mapperObj(source, EsHswhSaleHouse.class);
                hswhSaleHouse.setId(hit.getId());
                saleHouses.add(hswhSaleHouse);
            }
//            logger.info("从es中获取的二级索引：{}", rowkeys.toString());
//            List<HswhSaleHouse> hswhSaleHouses = hbaseDao.listObjByRowKeys(TableConstants.HSWH_SALE_HOUSE, rowkeys, HswhSaleHouse.class);
           hswhSaleQueryResponseEntity.setHswhSaleHouseListEntity(HswhSaleHouseCompose.composeListHouseListEntity1(saleHouses));
            if (saleHouses.size() == 0) {
                hswhSaleQueryResponseEntity.setTotalCount(String.valueOf(0));
            } else {
                hswhSaleQueryResponseEntity.setTotalCount(String.valueOf(hits.getTotalHits()));
            }
            hswhSaleQueryResponseEntity.setPageIndex(paramMap.get("pageIndex"));
        }
        return hswhSaleQueryResponseEntity;
    }

    @Override
    public List<HswhSaleHouseDetailDTO> listSaleHouseDetailByIds(Map<String, String> paramMap,String tableName, String
        ids, Class cls) throws Exception {
        paramMap.put("ids", ids);
        SearchResponse searchResponse = esDAO.searchHouseList(paramMap);
        logger.info("查询eslticsearch succee");
        List<HswhSaleHouse> hswhSaleHouses = this.hits2Json(searchResponse.getHits());
        return HswhSaleHouseCompose.composeDetailListDTO(hswhSaleHouses);
    }
    private List<HswhSaleHouse> hits2Json(SearchHits hits){
        List<HswhSaleHouse> hswhSaleHouses = new ArrayList<>();
        if (hits.getTotalHits() != 0) {
            for (SearchHit searchHit : hits.getHits()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", searchHit.getId());
                Map<String, Object> source = searchHit.getSource();
                for (String key : source.keySet()) {
                    jsonObject.put(key, source.get(key));
                }
                hswhSaleHouses.add(JSONObject.parseObject(jsonObject.toJSONString(), HswhSaleHouse.class));
            }
        }
        return hswhSaleHouses;
    }
}
